import { Gdx } from "./gdx";
import { Tscn, TscnExtResource, TscnNode, TscnSignal, TscnSubResource } from "./tscn";

class Gen {
    static tscn2gdx(scene: Tscn) {
        // 生成内容
        let outPut = `<GDScene load_steps="${scene.steps}" format="${scene.fromat.toString()}">`
        // ext
        scene.extResources.forEach(ext => {
            outPut += Gen.tscnExt2GdxExt(ext)
        })
        // sub
        scene.subResources.forEach(sub => {
            outPut += Gen.tscnSub2GdxSub(sub)
        })
        // node
        scene.nodeRoots.forEach(node => {
            outPut += Gen.tscnNode2GdxNode(node)
        })
        // signal
        scene.signals.forEach(signal => {
            outPut += Gen.tscnSignal2GdxSignal(signal)
        })
        outPut += "</GDScene>"
        return outPut
    }
    static tscnExt2GdxExt(ext: TscnExtResource) {
        let outPut = `<ExtRes path="${ext.path}" type="${ext.type}" id="${ext.id}"></ExtRes>`
        return outPut
    }
    static tscnSub2GdxSub(sub: TscnSubResource) {
        let outPut = `<SubRes type="${sub.type}" id="${sub.id}"`
        if (sub.props.length > 0) {
            outPut += "><Props>"
            sub.props.forEach(prop => {
                outPut += `<Prop name="${prop.name}">${prop.value}</Prop>`
            })
            outPut += `</Props>`
        }
        outPut += `</SubRes>`
        return outPut
    }


    static tscnNode2GdxNode(tnode: TscnNode) {
        let nodeType = tnode.type.length == 0
            ? "ResNode"
            : tnode.type.replace(/"/g, '')

        let outPut = `<${nodeType} name="${tnode.name}" ${tnode.parent == "-1" ? '' : "parent=\"" + tnode.parent+"\""}`
        // attr
        if (tnode.attrs.length > 0) {
            tnode.attrs.forEach(attr => {
                outPut += ` ${attr.name}="${attr.value}"`
            })
        }
        outPut += ">"
        // 先加prop
        if (tnode.props.length > 0) {
            outPut += "<Props>"
            tnode.props.forEach(prop => {
                outPut += `<Prop name="${prop.name}">${prop.value}</Prop>`
            })
            outPut += `</Props>`
        }

        if (tnode.children.length > 0) {
            outPut += "<Children>"
            tnode.children.forEach(node => {
                outPut += Gen.tscnNode2GdxNode(node);
            })
            outPut += `</Children>`
        }

        outPut += `</${nodeType}>`

        return outPut;
    }
    static tscnSignal2GdxSignal(signal: TscnSignal) {
        let outPut = `<Connection
            signal="${signal.signal}"
            from="${signal.from}"
            to="${signal.to}"
            method="${signal.method}"
        >`
        outPut += '</Connection>'
        return outPut
    }

    static gdx2Tscn(gdx: Gdx) {

    }
}

export {
    Gen
}